<template>
  <div class="page-shop-withdraw-cash">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="shopName"
            :label="$t('shop.storeName')+':'"
          >
            <el-input
              v-model="searchForm.shopName"
              type="text"
              clearable
              :placeholder="$t('shop.storeName')"
            />
          </el-form-item>
          <el-form-item
            v-if="paySettlementType!==1"
            prop="status"
            :label="$t('distributionMsg.auditStatus')+':'"
          >
            <el-select
              v-model="searchForm.status"
              clearable
              :placeholder="$t('distributionMsg.auditStatus')"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="searchChange(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
            <div
              class="default-btn primary-btn"
              @click="exportShopWithdrawCash()"
            >
              {{ $t('formData.export') }}
            </div>
          </el-form-item>
          <el-form-item>
            <div
              v-if="paySettlementType!==1"
              class="default-btn primary-btn"
              @click="setUpHandle()"
            >
              提现设置
            </div>
            <set-up
              v-if="dialogFormVisible"
              ref="setUpRef"
            />
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            :label="$t('sysManagement.serialNumber')"
            width="80"
          />
          <el-table-column
            :label="$t('shop.storeName')"
            prop="shopName"
          />
          <el-table-column
            :label="$t('shop.shopLogo')"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <ImgShow :src="scope.row.shopLogo" />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="amount"
            :label="$t('shopWithdrawCash.withdrawalAmount')"
          />
          <el-table-column
            prop="createTime"
            :label="paySettlementType===1? $t('distributionMsg.withdrawalTime') :$t('sys.creationTime')"
          />
          <el-table-column
            prop="updateTime"
            :label="$t('home.updateTime')"
          />
          <el-table-column
            v-if="paySettlementType!==1"
            prop="auditingTime"
            :label="$t('components.reviewTime')"
          >
            <template #default="scope">
              <span>{{ scope.row.auditingTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="paySettlementType!==1"
            :label="$t('distributionMsg.auditStatus')"
          >
            <template #default="scope">
              <span v-if="scope.row.status === 0">{{ $t('shop.underReview') }}</span>
              <span v-if="scope.row.status === 1">{{ $t('shopWithdrawCash.succeeded') }}</span>
              <span v-if="scope.row.status === -1">{{ $t('shopWithdrawCash.failed') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="paySettlementType!==1"
            fixed="right"
            align="center"
            :label="$t('crud.menu')"
            width="200"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('shop:shopWithdrawCash:audit') && scope.row.status === 0"
                  class="default-btn text-btn"
                  @click="auditHandle(scope.row)"
                >
                  {{ $t('productComm.audit') }}
                </div>
                <div
                  v-else-if="isAuth('shop:shopWithdrawCash:audit')"
                  class="default-btn text-btn"
                  @click="auditHandle(scope.row)"
                >
                  {{ $t('shopWithdrawCash.viewReviewInfo') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import addOrUpdate from './components/add-or-update.vue'
import SetUp from './components/set-up.vue'

import { ElMessage, ElLoading } from 'element-plus'
import { isAuth } from '@/utils'

const Data = reactive({
  theData: null, // 保存上次点击查询的请求条件

  dataList: [],
  page: {
    total: 0, // 总页数
    currentPage: 1, // 当前页数
    pageSize: 10 // 每页显示多少条
  },
  searchForm: {
    shopName: '',
    status: null
  }, // 搜索
  dataListLoading: false,
  addOrUpdateVisible: false,
  dialogFormVisible: false,
  status: [
    {
      label: $t('shop.underReview'),
      value: 0
    }, {
      label: $t('shopWithdrawCash.succeeded'),
      value: 1
    }, {
      label: $t('shopWithdrawCash.failed'),
      value: -1
    }
  ]
})

const allinpayStore = useAllinpayStore()
const paySettlementType = computed(() => {
  return allinpayStore.paySettlementType
})

const { dataList, page, searchForm, addOrUpdateVisible, dialogFormVisible, status } = toRefs(Data)

onMounted(() => {
  getDataList(Data.page)
})

const getDataList = (page, newData = false) => {
  Data.dataListLoading = true
  if (newData || !Data.theData) {
    Data.theData = JSON.parse(JSON.stringify(Data.searchForm))
  }
  http({
    url: http.adornUrl('/shop/shopWithdrawCash/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: page == null ? Data.page.currentPage : page.currentPage,
          size: page == null ? Data.page.pageSize : page.pageSize
        },
        Data.theData
      )
    )
  }).then(({ data }) => {
    Data.dataList = data.records
    Data.page.total = data.total
    Data.dataListLoading = false
  })
}

const setUpRef = ref()
const setUpHandle = () => {
  Data.dialogFormVisible = true
  nextTick(() => {
    setUpRef.value.open()
  })
}

const addOrUpdateRef = ref()
// 新增 / 修改
const auditHandle = (rowData) => {
  Data.addOrUpdateVisible = true
  nextTick(() => {
    addOrUpdateRef.value.init(rowData)
  })
}

/**
     * 刷新回调
     */
const refreshChange = () => {
  getDataList(Data.page)
}

const searchChange = (newData = false) => {
  Data.page.currentPage = 1
  getDataList(Data.page, newData)
}

const handleSizeChange = (val) => {
  Data.page.pageSize = val
  getDataList(Data.page)
}

const handleCurrentChange = (val) => {
  Data.page.currentPage = val
  getDataList(Data.page)
}

const searchFormRef = ref()
const resetForm = () => {
  searchFormRef.value.resetFields()
}

/**
     * 导出商家提现记录
     */
const exportShopWithdrawCash = () => {
  let params = {}
  params = JSON.parse(JSON.stringify(Data.searchForm))
  const loading = ElLoading.service({
    lock: true,
    target: '.mod-seckill-shopWithdrawCash',
    customClass: 'export-load',
    background: 'transparent',
    text: $t('shop.exportIng')
  })
  http({
    url: http.adornUrl('/shop/shopWithdrawCash/exportShopWithdrawCash'),
    method: 'get',
    params: http.adornParams(params),
    responseType: 'blob'
  }).then(({ data }) => {
    loading.close()
    const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
    const fileName = $t('shop.withDrawCashXls')
    const elink = document.createElement('a')
    if ('download' in elink) {
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
    } else {
      navigator.msSaveBlob(blob, fileName)
    }
    ElMessage({
      message: $t('shop.exportSuccess'),
      type: 'success',
      duration: 1500
    })
  }).catch((e) => {
    loading.close()
  })
}
</script>

<style lang="scss" scoped>
  :deep(.export-load) {
    top: -50% !important;
  }
</style>
